﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Hanhud.Win.Data
{
    public class PageDAL
    {
        public void Update(string module, string name, string title, string description, string keywords)
        {
            using (ConnectDB cnnDb = new ConnectDB())
            {
                string sql = "Pages_UpdatePage";
                
                SqlParameter modulePar = new SqlParameter("@Module", SqlDbType.VarChar, 50);
                SqlParameter namePar = new SqlParameter("@Name", SqlDbType.VarChar, 50);
                SqlParameter titlePar = new SqlParameter("@Title", SqlDbType.NVarChar, 256);
                SqlParameter descriptionPar = new SqlParameter("@Description", SqlDbType.NVarChar, 500);
                SqlParameter keywordsPar = new SqlParameter("@Keywords", SqlDbType.NVarChar, 500);

                modulePar.Value = module;
                namePar.Value = name;
                titlePar.Value = title;
                descriptionPar.Value = description;
                keywordsPar.Value = keywords;

                cnnDb.RunProcedure(sql, modulePar, namePar, titlePar, descriptionPar, keywordsPar);
                
            }            
        }

        public Page Get(string module, string name)
        {
            using (ConnectDB cnnDb = new ConnectDB())
            {
                string sql = "Pages_GetPage";

                SqlParameter namePar = new SqlParameter("@Name", SqlDbType.VarChar, 50);
                SqlParameter modulePar = new SqlParameter("@Module", SqlDbType.VarChar, 50);

                namePar.Value = name;
                modulePar.Value = module;   

                DataTable dt = cnnDb.GetDataTable(sql, CommandType.StoredProcedure, modulePar, namePar);

                if (dt.Rows.Count > 0)
                {
                    DataRow row = dt.Rows[0];

                    Page page = new Page();
                    page.Module = row["Module"].ToString();
                    page.Name = row["Name"].ToString();
                    page.Title = row["Title"].ToString();
                    page.Description = row["Description"].ToString();
                    page.Keywords = row["Keywords"].ToString();
                    int n;
                    page.PValue = int.TryParse(row["PValue"].ToString(), out n) ? n : 1;

                    return page;

                }
                else
                    return null;
                
            }
        }

        public
    }
}
